package com.nbsaas.boot.pay.ext.plugin;

import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.nbsaas.boot.pay.ext.domain.enums.PayType;
import com.nbsaas.boot.pay.ext.domain.request.PayBack;
import com.nbsaas.boot.pay.ext.domain.request.PayInfo;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


/**
 * 微信小程序支付.
 */
@Component("weiAppPaymentPlugin")
public class WeiAppPaymentPlugin {


    public static String getNonceStr() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }


    private String getOrderNo() {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Random rand = new Random();
        return format.format(new Date()) + "" + (rand.nextInt(99999) + 10000);
    }

    public ResponseObject<PayBack> payConfig(PayInfo payinfo) {

        ResponseObject<PayBack> back = new ResponseObject<PayBack>();
        PayBack result = new PayBack();
        back.setData(result);

        if (payinfo.getPayConfigType() == null) {
            payinfo.setPayConfigType(PayType.shop);
        }
        String platformId = payinfo.getPlatformId();
        String platformMchId = payinfo.getPlatformMchId();

        String subAppId = payinfo.getSubAppId();
        String sub_mch_id = payinfo.getSubMchId();

        String key = payinfo.getKey();
        String notify_url = payinfo.getNotifyUrl();

        WxPayService wxPayService = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();

        if (payinfo.getPayConfigType() == PayType.platform) {
            payConfig.setAppId(platformId);
            payConfig.setMchId(platformMchId);

            orderRequest.setSubAppId(subAppId);
            orderRequest.setSubMchId(sub_mch_id);
            orderRequest.setSubOpenid(payinfo.getOpenid());
            orderRequest.setMchId(platformMchId);
        } else {
            payConfig.setAppId(subAppId);
            payConfig.setMchId(sub_mch_id);
            orderRequest.setOpenid(payinfo.getOpenid());
            orderRequest.setMchId(sub_mch_id);
        }
        payConfig.setMchKey(key);
        wxPayService.setConfig(payConfig);


        orderRequest.setNotifyUrl(notify_url);
        orderRequest.setBody(payinfo.getBody());
        orderRequest.setOutTradeNo(getOrderNo());
        orderRequest.setTotalFee(payinfo.getMoney().multiply(new BigDecimal(100)).intValue());
        orderRequest.setTradeType("JSAPI");
        orderRequest.setSpbillCreateIp("123.12.12.123");
        try {
            WxPayUnifiedOrderResult orderResult = wxPayService.unifiedOrder(orderRequest);
            System.out.println(back);
            result.setNo(orderResult.getPrepayId());
            result.setOrderNo(orderRequest.getOutTradeNo());
        } catch (WxPayException e) {
            back.setCode(-301);
            back.setMsg(e.getReturnMsg());
            e.printStackTrace();
        }
        return back;
    }




    /**
     * MD5编码
     *
     * @param origin 原始字符串
     * @return 经过MD5加密之后的结果
     */
    public static String MD5Encode(String origin) {
        String resultString = null;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteArrayToHexString(md.digest(resultString.getBytes("utf-8")));//正确的写法
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    private static String byteArrayToHexString(byte[] digest) {

        StringBuffer hexValue = new StringBuffer();

        for (int i = 0; i < digest.length; i++) {
            int val = ((int) digest[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }

        return hexValue.toString();
    }

}
